<template>
  <div class="push">
    <button @click="clickStr">字符串路径</button>
    <button @click="clickPath">Path路径对象</button>
    <button @click="clickName">Name名称对象</button>

    <div class="push-view" v-if="showView">
      <div class="push-view__to">
        {{ $route.path }}
      </div>
      <div class="push-view__matched">
        {{ $route.matched[0].path }}
      </div>

      <div class="push-view__actual">
        <RouterView/>
      </div>
    </div>
  </div>
</template>

<script>
import {defineComponent} from 'vue';

export default defineComponent({
  name: 'Push',
  data() {
    return {
      showView: false,
    };
  },
  methods: {
    // 字符串路径
    clickStr() {
      this.showView = true;

      this.$router.push('/push-str');
    },
    // Path路径对象，配和query传递查询参数
    clickPath() {
      this.showView = true;

      // 若提供path则params将被忽略，正确传递params的方式是在path后直接加上路径参数
      this.$router.push({
        path: '/push-path/path',
        query: {
          query: 'path'
        }
      });
    },
    // Name名称对象，配合params传递路径参数
    clickName() {
      this.showView = true;

      // params只可传递string/number类型或为可重复参数传递数组类型，其他类型都将被自动字符串化，若为可选参数传递null或空字符串表示移除该路径参数
      this.$router.push({
        name: 'PushName',
        params: {
          params: 'name',
          repeat: [1, 2, 3]
        }
      });
    },
  }
});
</script>

<style scoped>
button {
  margin-right: 10px;
  margin-bottom: 20px;
}

.push-view {
  height: 120px;
  width: 320px;
  border: 1px silver solid;
  display: flex;
  flex-flow: column;
  align-items: center;
  row-gap: 20px;
  padding: 20px;

  & > .push-view__to {
    font-size: 25px;
    font-weight: bold;
  }

  & > .push-view__matched {
    font-size: 14px;
    font-weight: bold;
    color: grey;
  }

  & > .push-view__actual {
    font-weight: bold;
    color: dodgerblue;
  }
}
</style>